import 'package:flutter/material.dart';
import 'package:flutter_liquid_glass/liquid_glass.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/utils/theme/style_utils.dart';

///按钮事件的回调
typedef GBAlertCallBack = void Function();

class GBAlert {
  /// 使用Get.back(result:T)回传数据
  static Future<T?> show<T>({
    required String title,
    GBAlertButtonType buttonType = GBAlertButtonType.twoButtonHorizontal,
    required Widget content,
    double? width,
    double? height,
    double? radius,
    String? commitButtonText,
    String? cancelButtonText,
    GBAlertCallBack? commitCallBack,
    GBAlertCallBack? cancelCallBack,
    bool canClose = true,
    String? tag,
    //突出颜色
    Color? highlightColor,
  }) {
    return Get.dialog(
      barrierDismissible: false, // 设置为 false 将禁用返回键和点击背景关闭
      Scaffold(
        backgroundColor: Colors.transparent,
        body: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
          return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Center(
                child: Container(
                  width: width ?? 280.r,
                  height: height ?? 130.r,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(radius ?? 12.r),
                  ),
                  child: LiquidGlassContainer(
                    config: LiquidGlassConfig(
                      baseColor: ColorUtils.themeColor,
                      opacity: 0.4,
                      blurAmount: 10,
                      borderRadius: BorderRadius.circular(30),
                      enableSpecularHighlight: true,
                      refractionIntensity: 0.8,
                      gradient: LinearGradient(
                        colors: [
                          ColorUtils.themeColor.withValues(alpha: 0.1),
                          ColorUtils.themeColor.withValues(alpha: 0.1),
                        ],
                      ),
                      shadows: [
                        BoxShadow(
                          color: ColorUtils.themeColor.withValues(alpha: 0.2),
                          blurRadius: 20,
                          offset: const Offset(0, 10),
                        ),
                      ],
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 15.r, vertical: 15.r),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Column(
                          children: [
                            SizedBox(height: 5.r),
                            Text(title, style: StyleUtils.ts_ff_17_500),
                            SizedBox(height: 15.r),
                            content,
                            SizedBox(height: 15.r),
                          ],
                        ),
                        Column(
                          children: [
                            Visibility(
                              visible: (buttonType == GBAlertButtonType.twoButtonHorizontal),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  GestureDetector(
                                    behavior: HitTestBehavior.opaque,
                                    onTap: () {
                                      //确定关闭弹框之后再回调
                                      if (canClose) {
                                        Get.back();
                                      }
                                      Future.delayed(Duration(milliseconds: 100), () {
                                        cancelCallBack?.call();
                                      });
                                    },
                                    child: Container(
                                      width: 120.r,
                                      height: 40.r,
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(20.r),
                                        border: Border.all(
                                          color: highlightColor ?? ColorUtils.themeColor,
                                          width: 0.5,
                                        ),
                                      ),
                                      child: Center(
                                        child: Text(
                                          cancelButtonText ?? "cancel".tr,
                                          style: StyleUtils.ts_theme_15_600.copyWith(color: highlightColor ?? ColorUtils.themeColor),
                                        ),
                                      ),
                                    ),
                                  ),
                                  GestureDetector(
                                    behavior: HitTestBehavior.opaque,
                                    onTap: () {
                                      if (canClose) {
                                        Get.back();
                                      }
                                      Future.delayed(Duration(milliseconds: 100), () {
                                        commitCallBack?.call();
                                      });
                                    },
                                    child: Container(
                                      width: 120.r,
                                      height: 40.r,
                                      decoration: BoxDecoration(
                                        color: highlightColor ?? ColorUtils.themeColor,
                                        borderRadius: BorderRadius.circular(20.r),
                                      ),
                                      child: Center(
                                        child: Text(commitButtonText ?? "commit".tr, style: StyleUtils.ts_ff_15_600),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Visibility(
                              visible: (buttonType == GBAlertButtonType.oneButton || buttonType == GBAlertButtonType.twoButtonVertical),
                              child: GestureDetector(
                                behavior: HitTestBehavior.opaque,
                                onTap: () {
                                  if (canClose) {
                                    Get.back();
                                  }
                                  Future.delayed(Duration(milliseconds: 100), () {
                                    commitCallBack?.call();
                                  });
                                },
                                child: Container(
                                  height: 40.r,
                                  decoration: BoxDecoration(
                                    color: highlightColor ?? ColorUtils.themeColor,
                                    borderRadius: BorderRadius.circular(20.r),
                                  ),
                                  child: Center(
                                    child: Text(commitButtonText ?? "i_got_it".tr, style: StyleUtils.ts_ff_17_500),
                                  ),
                                ),
                              ),
                            ),
                            Visibility(
                              visible: (buttonType == GBAlertButtonType.twoButtonVertical),
                              child: GestureDetector(
                                behavior: HitTestBehavior.opaque,
                                onTap: () {
                                  if (canClose) {
                                    Get.back();
                                  }
                                  Future.delayed(Duration(milliseconds: 100), () {
                                    cancelCallBack?.call();
                                  });
                                },
                                child: Container(
                                  height: 40.r,
                                  decoration: BoxDecoration(
                                    border: Border.all(
                                      color: highlightColor ?? ColorUtils.themeColor,
                                      width: 0.5,
                                    ),
                                    borderRadius: BorderRadius.circular(20.r),
                                  ),
                                  child: Center(
                                    child: Text(cancelButtonText ?? "cancel".tr, style: StyleUtils.ts_ff_17_500),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ],
          );
        }),
      ),
      routeSettings: RouteSettings(name: tag != null ? 'sheet_alert_${tag}' : 'sheet_alert'),
    );
  }
}

///按钮排列方式
enum GBAlertButtonType {
  //1单按钮  2横向两个按钮 3竖向两个按钮
  oneButton,
  twoButtonHorizontal,
  twoButtonVertical,
}
